package com.htjx.mall.admin.controllers;

import java.io.File;
import java.io.IOException;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.htjx.mall.commons.api.CommonConstant;
import com.htjx.mall.commons.api.Result;

@Controller
@RequestMapping("/admin/data")
public class _DataController {
	
	@RequestMapping(value="/operation", method = RequestMethod.GET)
	public String operation(Model model) {
		return "data/operation";
	}
	
	@RequestMapping(value="/backup", method = RequestMethod.POST)
	public String backup(HttpServletRequest request, Model model) {
		model.addAttribute("action", "backup");
		
		String filePath = CommonConstant.SHARE_PATH;
		String realPath = request.getSession().getServletContext().getRealPath(filePath); 
		model.addAttribute("realPath", realPath);
		File srcDir = new File(realPath);    
	    //如果文件夹不存在则返回
	    if (!srcDir.exists() && !srcDir.isDirectory())      
	    {
	    	Result result = new Result(9, "文件夹不存在");
	    	model.addAttribute("result", result);
	    	return "data/result";
	    }
		
		String backupFilePath = CommonConstant.BACKUP_SHARE_PATH;
		String backupRealPath = request.getSession().getServletContext().getRealPath(backupFilePath); 
		model.addAttribute("backupRealPath", backupRealPath);
		
	    File destDir = new File(backupRealPath);    
	    //如果文件夹不存在则创建    
	    if (!destDir.exists() && !destDir.isDirectory())      
	    {
	    	try {
				FileUtils.forceMkdir(destDir);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		
	    try {
			FileUtils.copyDirectory(srcDir, destDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    Result result = new Result(0, "备份成功");
	    model.addAttribute("result", result);
		return "data/result";
	}
	
	@RequestMapping(value="/restore", method = RequestMethod.POST)
	public String restore(HttpServletRequest request, Model model) {
		model.addAttribute("action", "restore");
		
		String backupFilePath = CommonConstant.BACKUP_SHARE_PATH;
		String backupRealPath = request.getSession().getServletContext().getRealPath(backupFilePath); 
		model.addAttribute("backupRealPath", backupRealPath);
		
		File srcDir = new File(backupRealPath);    
	    //如果文件夹不存在则返回
	    if (!srcDir.exists() && !srcDir.isDirectory())      
	    {
	    	Result result = new Result(9, "文件夹不存在");
	    	model.addAttribute("result", result);
	    	return "data/result";
	    }
		
	    String filePath = CommonConstant.SHARE_PATH;
		String realPath = request.getSession().getServletContext().getRealPath(filePath); 
		model.addAttribute("realPath", realPath);
		
	    File destDir = new File(realPath);    
	    //如果文件夹不存在则创建    
	    if (!destDir.exists() && !destDir.isDirectory())      
	    {
	    	try {
				FileUtils.forceMkdir(destDir);
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	    }
		
	    try {
			FileUtils.copyDirectory(srcDir, destDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    Result result = new Result(0, "恢复成功");
	    model.addAttribute("result", result);
		return "data/result";
	}
	@RequestMapping(value="/clear", method = RequestMethod.POST)
	public String clear(HttpServletRequest request, Model model) {
		model.addAttribute("action", "clear");
		
		String filePath = CommonConstant.SHARE_PATH;
		String realPath = request.getSession().getServletContext().getRealPath(filePath); 
		model.addAttribute("realPath", realPath);
		File srcDir = new File(realPath);    
	    //如果文件夹不存在则返回
	    if (!srcDir.exists() && !srcDir.isDirectory())      
	    {
	    	Result result = new Result(9, "文件夹不存在");
	    	model.addAttribute("result", result);
	    	return "data/result";
	    }
		
		try {
			FileUtils.cleanDirectory(srcDir);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		Result result = new Result(0, "清除成功");
	    model.addAttribute("result", result);
		return "data/result";
	}
}
